package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理等级Service业务层处理
 *
 * @author ityu
 * @date 2025-10-13
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper,NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private INursingPlanService nursingPlanService;


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    private final  String NURSING_LEVEL_CACHE = "nursing_level_cache";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevelVo selectNursingLevelById(Integer id)
    {
        NursingLevel nursingLevel = this.getById(id);
        NursingLevelVo result = BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
        result.setId(nursingLevel.getId().toString());
        result.setLplanId(nursingLevel.getLplanId().toString());
        return result;
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        nursingLevels = nursingLevels.stream().map(nursingL -> {
            NursingPlan nursingPlan = nursingPlanService.getById(nursingL.getLplanId());
            if (nursingPlan != null) {
                nursingL.setPlanName(nursingPlan.getPlanName());
            }
                return nursingL;
        }).collect(Collectors.toList());
        return nursingLevels;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        deleteCache();
        return this.save(nursingLevel) == true?1:0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        deleteCache();
        return  this.updateById(nursingLevel) == true?1:0;
    }


    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Integer id)
    {
        deleteCache();
        return this.removeById(id) == true?1:0;
    }

    @Override
    public List<NursingLevel> getAll() {
        List<NursingLevel> nursingLevels =(List<NursingLevel>) redisTemplate.opsForValue().get(NURSING_LEVEL_CACHE);

        if (CollectionUtils.isNotEmpty(nursingLevels)){
            return nursingLevels;
        }
        nursingLevels = this.list(Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getStatus, 1));
        redisTemplate.opsForValue().set(NURSING_LEVEL_CACHE,nursingLevels);
        return nursingLevels;
    }

    private  void deleteCache(){
        redisTemplate.delete(NURSING_LEVEL_CACHE);
    }
}
